﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ELS.User
{
    class GrantDataAccess
    {
        string connectionString = UserCommon.connectionString;

        internal int DeleTeAllPermissionOfGroup(string currentGroupId)
        {
            int rowDelete = 0;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlComm = new SqlCommand();
                    sqlComm = sqlConnection.CreateCommand();
                    sqlComm.CommandText = @"DELETE FROM GroupPermission
                                            WHERE GroupPermission.GROUP_ID = @groupId";

                    sqlComm.Parameters.Add("@groupId", SqlDbType.Int).Value = currentGroupId;
                    rowDelete = sqlComm.ExecuteNonQuery();
                    return rowDelete;
                }
                catch (SqlException e)
                {
                    return rowDelete;
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
        }

        internal int InsertNewPermissionOfGroup(string currentGroupId,List<int> newPermissions)
        {
            int rowInsert = 0;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                try
                {
                    sqlConnection.Open();
                    int i = 0;
                    for ( i = 0; i < newPermissions.Count; i++)
                    {
                        SqlCommand sqlComm = new SqlCommand();
                        sqlComm = sqlConnection.CreateCommand();
                        sqlComm.CommandText = @"INSERT INTO GroupPermission (GROUP_ID,PERMISSION_ID)
                                            VALUES (@groupId,@permissionId)";
                        sqlComm.Parameters.Add("@groupId", SqlDbType.Int).Value = currentGroupId;
                        sqlComm.Parameters.Add("@permissionId", SqlDbType.Int).Value = newPermissions[i];
                        rowInsert = sqlComm.ExecuteNonQuery();
                    }
                    return rowInsert = i;
                }
                catch (SqlException e)
                {
                    return rowInsert;
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
        }

        internal int DeleteUsingGrpPerInListByGroupId(string listGroupId)
        {
            int rowDelete = -1;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlComm = new SqlCommand();
                    sqlComm = sqlConnection.CreateCommand();
                    sqlComm.CommandText = @"DELETE FROM GroupPermission  
                                               WHERE GroupPermission.GROUP_ID IN ("+listGroupId+")";
                    rowDelete = sqlComm.ExecuteNonQuery();
                    return rowDelete;
                }
                catch (SqlException e)
                {
                    return rowDelete;
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
        }
        internal int DeleteUsingGrpPerInListByPermissionId(string listPermissionId)
        {
            int rowDelete = -1;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                try
                {
                    sqlConnection.Open();
                    SqlCommand sqlComm = new SqlCommand();
                    sqlComm = sqlConnection.CreateCommand();
                    sqlComm.CommandText = @"DELETE FROM GroupPermission  
                                               WHERE GroupPermission.PERMISSION_ID IN (" + listPermissionId + ")";
                    rowDelete = sqlComm.ExecuteNonQuery();
                    return rowDelete;
                }
                catch (SqlException e)
                {
                    return rowDelete;
                }
                finally
                {
                    sqlConnection.Close();
                }
            }
        }
    }
}
